import axios from 'axios'

// 创建axios实例
const api = axios.create({
  baseURL: 'http://localhost:8080/LZ', 
  timeout: 10000,
  withCredentials: true  // 允许跨域请求携带cookie
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    return Promise.reject(error)
  }
)

// 用户相关API
export const userApi = {
  // 获取验证码
  getKaptcha() {
    return api.get('/club/kaptcha', { responseType: 'blob' })
  },
  
  // 用户登录
  login(data) {
    return api.post('/club/login', data)
  },
  
  // 用户注册
  register(data) {
    return api.post('/club/register', data, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },
  
  // 重置密码
  resetPassword(data) {
    return api.post('/club/resetPassword', data)
  },

  // 退出登录
  logout() {
    return api.post('/club/logout')
  }
}

// 新增社团相关API
export const clubApi = {
  // 获取社团列表
  getClubList(data, userId) {
    return api.post('/userclubs/list', data, {
      params: {
        userId
      }
    })
  },
  
  // 获取社团详情
  getClubDetail(clubId) {
    return api.get('/userclubs/selectClubsDetail', {
      params: {
        clubId
      }
    })
  },
  
  // 获取社团成员和活动
  getClubMembersAndActivities(clubId) {
    return api.get('/userclubs/selectClubsMembers', {
      params: {
        clubId
      }
    })
  },

  // 加入社团
  joinClub(clubId, username) {
    return api.post('/userclubs/joinClub', null, {
      params: {
        clubId,
        username
      }
    })
  },

  // 获取我的社团列表
  getMyClubs(userId) {
    return api.get('/userclubs/selectClubs', {
      params: {
        userId
      }
    })
  }, 
  // 退出社团
  quitClub(clubId, userId) {
    return api.post('/userclubs/quitClub', null, {
      params: {
        clubId,
        userId
      }
    })
  },
  
  // 社团创建进度查询
  selectClubsCreateProgress(userId) {
    return api.get('/userclubs/selectClubsCreateProgress', {
      params: {
        userId
      }
    })
  },

  // 获取社团活动列表
  getClubActivities(clubId) {
    return api.get('/userclubs/selectActivities', {
      params: {
        clubId
      }
    })
  },

  // 报名活动
  signUpActivity(activityId, userId) {
    return api.post('/userclubs/signUpActivity', null, {
      params: {
        activityId,
        userId
      }
    })
  },

  // 获取所有活动列表
  getAllActivities(data) {
    return api.post('/userclubs/selectAllActivities', data)
  },
  // 获取用户报名的活动列表
  getUserSignUpActivities(userId, status, keyword) {
    return api.get('/userclubs/selectUserSignUpActivities', {
      params: {
        userId,
        status,
        keyword
      }
    })
  },
  // 创建社团
  createClub(formData) {
    return api.post('/userclubs/CreateCommunity', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

export default api